clear 
clear matrix 
set more off  
set mem 750000

** Use data with 2012 field ** 
use fo_replication.dta, replace 

** Indicator for States (for state FEs in models below) ** 
encode state, generate(state_numeric) 

** 2004 Battleground status (from Shaw 2007, p.66) ** 
gen ds_04bg = 0 
replace ds_04bg = 1 if state =="Florida" | state=="Iowa" | state=="New Hampshire" | state == "New Mexico" | state=="Oho" | state == "Pennsylvania" | state=="Wisconsin" 

** 2008 revised battleground (from KHJ volume) ** 
gen bg08_khj = 0 
replace bg08_khj = 1 if state=="Nevada" | state=="Montana" | state=="Colorado" | state=="New Mexico" | state=="Wisconsin" | state=="Iowa" | state=="Missouri" | state=="Michigan" | state=="Indiana" | state=="Ohio" | state=="Pennsylvania" | state=="Virginia" | state=="New Hampshire" | state=="North Carolina" | state=="Florida"

** 2012 Battleground States (from NYT Times, 5 May 22 "9 Swing States, Critical to Presidential Race, Are Mixed Lot")
gen bg12_nyt = 0
replace bg12_nyt = 1 if state =="Colorado" | state=="Florida" | state =="Iowa" | state=="Nevada" | state=="New Hampshire" | state=="Ohio" | state=="Pennsylvania" | state=="Virginia" | state=="Wisconsin"

************************************************
** 2012 Placement:                            **
** How Do Romney/Obama Respond to Each Other? **
** Table 1                                    ** 
************************************************

** Generate normal vote measure: pct Rep averaged over 2000, 2004, and 2008 ** 
egen rep_nv = rmean(bush00pc bushpc mccainpc) 

** rescale some demographics to control the # of digits in table 1 ** 
gen rs_medinc08 = medinc08/100000 
gen rs_pop08 = pop2008/1000000 
gen popsq = rs_pop08^2
gen rs_medage08 = medage08/100
gen rs_black = black/100
gen rs_hispanic = hispanic/100

** Controlling for Normal Vote ** 
reg obama12fo romney12fo rep_nv bg12_nyt rs_medage08 rs_pop08 popsq rs_medinc08 rs_black rs_hispanic pc_less_hs00 pc_degree00 i.state_numeric
outreg2 using placement12.doc, se noaster dec(3) 
reg romney12fo obama12fo rep_nv bg12_nyt rs_medage08 rs_pop08 popsq rs_medinc08 rs_black rs_hispanic pc_less_hs00 pc_degree00 i.state_numeric 
outreg2 using placement12.doc, se noaster dec(3)  

/* Definition of swing: change in winner 04 vs. 08 */ 
gen flip = 0 
replace flip = 1 if bushpc < .5 & mccainpc > .5 
replace flip = 1 if bushpc > .5 & mccainpc < .5
gen solid_blue = 0 
replace solid_blue = 1 if bushpc < .5 & mccainpc <.5  
gen solid_red = 0 
replace solid_red = 1 if bushpc > .5 & mccainpc > .5 

/* core vs. swing */ 
reg obama12fo romney12fo flip solid_blue bg12_nyt rs_medage08 rs_pop08 popsq rs_medinc08 rs_black rs_hispanic pc_less_hs00 pc_degree00 i.state_numeric
outreg2 using placement12.doc, se noaster dec(3)
lincom solid_blue - flip /* more in core counties (big difference) */ 
reg romney12fo obama12fo flip solid_red bg12_nyt rs_medage08 rs_pop08 popsq rs_medinc08 rs_black rs_hispanic pc_less_hs00 pc_degree00 i.state_numeric
outreg2 using placement12.doc, se noaster dec(3) 
lincom solid_red - flip /* no difference */ 

/* matching: core vs. swing */ 
reg obama12fo flip##c.romney12fo solid_red##c.romney12fo bg12_nyt rs_medage08 rs_pop08 popsq rs_medinc08 rs_black rs_hispanic pc_less_hs00 pc_degree00 i.state_numeric
outreg2 using placement12.doc, se noaster dec(3)
reg romney12fo flip##c.obama12fo solid_blue##c.obama12fo bg12_nyt rs_medage08 rs_pop08 popsq rs_medinc08 rs_black rs_hispanic pc_less_hs00 pc_degree00 i.state_numeric
outreg2 using placement12.doc, se noaster dec(3) 
/* different effects: Romney matches close counties, Obama doubles down on core counties */ 
/* difference in battleground states */ 
reg obama12fo flip##c.romney12fo solid_red##c.romney12fo bg12_nyt medage08 rs_pop08 popsq rs_medinc08 black hispanic pc_less_hs00 pc_degree00 i.state_numeric if bg12_nyt == 1
reg romney12fo flip##c.obama12fo solid_blue##c.obama12fo bg12_nyt medage08 rs_pop08 popsq rs_medinc08 black hispanic pc_less_hs00 pc_degree00 i.state_numeric if bg12_nyt == 1
/* same pattern as above */ 

**********************************
** Prep Data for Panel Analysis **
**********************************

** Rename variables so they don't end with numbers (for panel analysis) ** 
rename totalvote04 v_04totalvote 
rename totalvote08 v_08totalvote 
rename totalvote12 v_12totalvote
rename pop2004 d_2004pop
rename pop2008 d_2008pop 
 
rename dumfo04 f_04dumfo 
rename dumfo08 f_08dumfo 
gen f_12dumfo = 0 
replace f_12dumfo = 1 if obama12fo > 0

** Calculate Turnout (# of voters/census population) ** 
gen pct_04turnout = v_04totalvote/d_2004pop
replace pct_04turnout = . if pct_04turnout > 1 /* Loving County, TX: v. small, slightly more voters than census residents */ 
gen pct_08turnout = v_08totalvote/d_2008pop 
replace pct_08turnout = . if pct_08turnout > 1 
gen pct_12turnout = v_12totalvote/d_2008pop 
replace pct_12turnout = . if pct_12turnout > 1 

** Rename for panel analysis ** 
rename ds_04bg bg1 
rename bg08_khj bg2 
rename bg12_nyt bg3  
rename kerryfo field1 
rename obamafo field2
rename obama12fo field3  
rename f_04dumfo binary_field1 
rename f_08dumfo binary_field2
rename f_12dumfo binary_field3 
rename kerrypc dempc1 
rename obamapc dempc2  
rename obamapct12 dempc3 
rename kerry dem1 
rename obama dem2 
rename obama12 dem3 
rename pct_04turnout turnout1 
rename pct_08turnout turnout2 
rename pct_12turnout turnout3 
rename bush00pc lagrep1 
rename bushpc lagrep2 
rename mccainpc lagrep3 

gen yr1 = 0 
gen yr2 = 1 
gen yr3 = 2 

reshape long bg dempc dem lagrep field binary_field turnout yr, i(fips) j(year) 
xtset fips year 

/* Standard Fixed Effects Analysis */ 
xtreg turnout binary_field i.state_numeric#year, fe vce(r) 
xtreg dempc binary_field i.state_numeric#year, fe vce(r)  

/* Interaction with Battleground State */ 
xtreg turnout binary_field##bg i.state_numeric#year, fe vce(r) 
xtreg dempc binary_field##bg i.state_numeric#year, fe vce(r)  

/* Interaction with County Population */ 
egen popbar = mean(d_2008pop/100000) 
gen rs_pop = (d_2008pop/100000) - popbar /* mean-deviate county-population */ 
xtreg turnout binary_field##c.rs_pop i.state_numeric#year, fe vce(r) 
xtreg dempc binary_field##c.rs_pop i.state_numeric#year, fe vce(r) 
